<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2006/09/11 22:32:16 $)
// $RCSfile: admin_sla.php,v $ : $Revision: 1.9 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

global $_sladays;
$_sladays = array("sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday");
require_once ("./modules/tickets/functions_sla.php");

require_once ("./includes/functions_html.php");
$template->loadLanguageSection("tickets");

/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing schedule data
*/
function _scheduleFields($arg)
{
	global $_SWIFT, $_sladays;
	$record = &$arg[0];
	$fields = &$arg[1];

	$_desc = '<table width="100%"  border="0" cellspacing="1" cellpadding="2">'. SWIFT_CRLF;
	foreach ($_sladays as $key=>$val)
	{
		if ($record[$val."_enabled"] == 1)
		{
			$hourstatus = returnParsedHour($record[$val."_open"])." => ". returnParsedHour($record[$val."_close"]);
		} else {
			$hourstatus = $_SWIFT["language"]["closed"];
		}
		$_desc .= '<tr class="smalltext"><td width="80"><b>'. htmlspecialchars($_SWIFT["language"][$val]) .':&nbsp;</b></td><td>'. $hourstatus .'</td></tr>'.SWIFT_CRLF;
	}
	$_desc .= '</table>'.SWIFT_CRLF;

	$record["scheduledesc"] = $_desc;

	$record["options"] = '<a href="index.php?_m=tickets&_a=editschedule&slascheduleid='. $record["slascheduleid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=tickets&_a=manageschedules&do=delete&slascheduleid='. $record["slascheduleid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Callback for parsing plan data
*/
function _planFields($arg)
{
	global $_SWIFT, $_sladays;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["slaplans.title"] = '<a href="index.php?_m=tickets&_a=editplan&slaplanid='. $record["slaplanid"] . '" title="'. $record["title"] .'">'. $record["title"] .'</a>';

	if (empty($record["departmenttitle"]))
	{
		$record["departments.title"] = $_SWIFT["language"]["alldepartments"];
	} else {
		$record["departments.title"] = $record["departmenttitle"];
	}
	
	$record["slaschedules.title"] = $record["scheduletitle"];
	$record["slaplans.overduehrs"] = $record["overduehrs"];

	$record["options"] = '<a href="index.php?_m=tickets&_a=editplan&slaplanid='. $record["slaplanid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=tickets&_a=manageplans&do=delete&slaplanid='. $record["slaplanid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';

	return $record;
}

/**
* Mass Action: Delete SLA Plans
*/
function _maDeleteSLAPlans()
{
	global $dbCore, $_SWIFT;

	if ($_SWIFT["isdemo"] == true)
	{
		return false;
	}

	if (is_array($_POST["itemid"]))
	{
		deleteSLAPlans($_POST["itemid"]);
	}
}

/**
* Mass Action: Delete SLA Schedules
*/
function _maDeleteSLASchedules()
{
	global $dbCore, $_SWIFT;

	if ($_SWIFT["isdemo"] == true)
	{
		return false;
	}

	if (is_array($_POST["itemid"]))
	{
		$result = deleteSLASchedules($_POST["itemid"]);
		if (!$result)
		{
			$_SWIFT["errormessage"] = $_SWIFT["language"]["scheduledeletefailure"];			
		}
	}
}

/**
* ###############################################
* MANAGE SLA PLANS
* ###############################################
*/
if ($eventaction == "manageplans")
{
	if ($_GET["do"] == "delete" && !empty($_GET["slaplanid"]) && $_SWIFT["isdemo"] != true)
	{
		$infomessage = sprintf($_SWIFT["language"]["slaplandeleteconfirm"], $_SWIFT["slaplancache"][$_GET["slaplanid"]]["title"]);

		deleteSLAPlans(array($_GET["slaplanid"]));
	}

	if (!empty($_REQUEST["insertplan"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["planinsertconfirm"], $_REQUEST["insertplan"]);
	} else if (!empty($_REQUEST["updateplan"])) {
		$infomessage = sprintf($_SWIFT["language"]["planupdateconfirm"], $_REQUEST["updateplan"]);
	}

	$options["recordsperpage"] = "6";
	$options["sortby"] = "slaplans.title";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteSLAPlans";

	$options["idname"] = "slaplanid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["plantitle"];
	$options["advancedsearch"][0]["query"] = 'SELECT slaplans.*, slaschedules.title AS scheduletitle, departments.title AS departmenttitle FROM `'. TABLE_PREFIX .'slaplans` AS slaplans LEFT JOIN `'. TABLE_PREFIX .'slaschedules` AS slaschedules ON (slaplans.slascheduleid = slaschedules.slascheduleid) LEFT JOIN `'. TABLE_PREFIX .'departments` AS departments ON (slaplans.departmentid = departments.departmentid) WHERE slaplans.title LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_planFields";

	$fields[0]["name"] = "slaplans.title";
	$fields[0]["title"] = $_SWIFT["language"]["plantitle"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "departments.title";
	$fields[1]["title"] = $_SWIFT["language"]["department"];
	$fields[1]["width"] = "160";
	$fields[1]["align"] = "center";

	$fields[2]["name"] = "slaschedules.title";
	$fields[2]["title"] = $_SWIFT["language"]["scheduletitle"];
	$fields[2]["width"] = "160";
	$fields[2]["align"] = "center";

	$fields[3]["name"] = "slaplans.overduehrs";
	$fields[3]["title"] = $_SWIFT["language"]["overduehrs"];
	$fields[3]["width"] = "140";
	$fields[3]["align"] = "center";

	$fields[4]["type"] = "custom";
	$fields[4]["name"] = "options";
	$fields[4]["title"] = $_SWIFT["language"]["options"];
	$fields[4]["width"] = "150";
	$fields[4]["align"] = "center";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["sla"]." > ".$_SWIFT["language"]["manageplans"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageplans" title="'.$_SWIFT["language"]["slaext"].'">'.$_SWIFT["language"]["slaext"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageplans" title="'.$_SWIFT["language"]["manageplans"].'">'.$_SWIFT["language"]["manageplans"].'</a>', $_SWIFT["language"]["desc_sla"] , 6);
	printInfoBox($infomessage);

	$grid->start("slaplans", $_SWIFT["language"]["slaplanlist"], 'SELECT slaplans.*, slaschedules.title AS scheduletitle, departments.title AS departmenttitle FROM `'. TABLE_PREFIX .'slaplans` AS slaplans LEFT JOIN `'. TABLE_PREFIX .'slaschedules` AS slaschedules ON (slaplans.slascheduleid = slaschedules.slascheduleid) LEFT JOIN `'. TABLE_PREFIX .'departments` AS departments ON (slaplans.departmentid = departments.departmentid) $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'slaplans`;', 'SELECT slaplans.*, slaschedules.title AS scheduletitle, departments.title AS departmenttitle FROM `'. TABLE_PREFIX .'slaplans` AS slaplans LEFT JOIN `'. TABLE_PREFIX .'slaschedules` AS slaschedules ON (slaplans.slascheduleid = slaschedules.slascheduleid) LEFT JOIN `'. TABLE_PREFIX .'departments` AS departments ON (slaplans.departmentid = departments.departmentid) WHERE slaplans.title LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	$grid->display("slaplans");
	
	$interface->adminFooter();






/**
* ###############################################
* INSERT SLA PLAN
* ###############################################
*/
} else if ($eventaction == "insertplan") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["overduehrs"]) == "" || trim($_POST["slascheduleid"]) == "" || trim($_POST["departmentid"]) == "" || trim($_POST["ticketstatusid"]) == "" || trim($_POST["priorityid"]) == "" || !_is_array($_POST["priorityid"]) || !_is_array($_POST["ticketstatusid"]))
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			if (count($_POST["ticketstatusid"]) == 1 && $_POST["ticketstatusid"][0] == 0)
			{
				// Build all ticket status ids
				$_POST["ticketstatusid"] = array();
				$dbCore->query("SELECT `ticketstatusid` FROM `". TABLE_PREFIX ."ticketstatus`;");
				while ($dbCore->nextRecord())
				{
					$_POST["ticketstatusid"][] = $dbCore->Record["ticketstatusid"];
				}
			}

			if (count($_POST["priorityid"]) == 1 && $_POST["priorityid"][0] == 0)
			{
				$_POST["priorityid"] = array();
				$dbCore->query("SELECT `priorityid` FROM `". TABLE_PREFIX ."ticketpriorities`;");
				while ($dbCore->nextRecord())
				{
					$_POST["priorityid"][] = $dbCore->Record["priorityid"];
				}
			}
			$slaplanid = insertSLAPlan($_POST["title"], $_POST["overduehrs"], $_POST["departmentid"], $_POST["slascheduleid"], $_POST["ticketstatusid"], $_POST["priorityid"]);

			printRedirect(sprintf($_SWIFT["language"]["planinsertconfirm"], $_POST["title"]), "index.php?_m=tickets&_a=manageplans&insertplan=".urlencode($_POST["title"]));
			exit;
		}
	}

	// ======= Start Interface Output =======
	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["sla"]." > ".$_SWIFT["language"]["insertslaplan"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageplans" title="'.$_SWIFT["language"]["slaext"].'">'.$_SWIFT["language"]["slaext"].'</a> &raquo; <a href="index.php?_m=tickets&_a=insertplan" title="'.$_SWIFT["language"]["insertplan"].'">'.$_SWIFT["language"]["insertplan"].'</a>', $_SWIFT["language"]["desc_sla"] , 6);
	
	// Check for schedules
	if (!count($_SWIFT["slaschedulecache"]))
	{
		$errormessage = iif($errormessage=="", $_SWIFT["language"]["errorinsertschedule"], $errormessage);
	}

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderSLAForm(INSERT);

	$template->assign("backurl", "index.php?_m=tickets&_a=manageplans");

	$interface->adminFooter();





/**
* ###############################################
* EDIT SLA PLAN
* ###############################################
*/
} else if ($eventaction == "editplan") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "" || trim($_POST["overduehrs"]) == "" || trim($_POST["slascheduleid"]) == "" || trim($_POST["departmentid"]) == "" || trim($_POST["ticketstatusid"]) == "" || trim($_POST["priorityid"]) == "" || !_is_array($_POST["priorityid"]) || !_is_array($_POST["ticketstatusid"]))
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			if (count($_POST["ticketstatusid"]) == 1 && $_POST["ticketstatusid"][0] == 0)
			{
				// Build all ticket status ids
				$_POST["ticketstatusid"] = array();
				$dbCore->query("SELECT `ticketstatusid` FROM `". TABLE_PREFIX ."ticketstatus`;");
				while ($dbCore->nextRecord())
				{
					$_POST["ticketstatusid"][] = $dbCore->Record["ticketstatusid"];
				}
			}

			if (count($_POST["priorityid"]) == 1 && $_POST["priorityid"][0] == 0)
			{
				$_POST["priorityid"] = array();
				$dbCore->query("SELECT `priorityid` FROM `". TABLE_PREFIX ."ticketpriorities`;");
				while ($dbCore->nextRecord())
				{
					$_POST["priorityid"][] = $dbCore->Record["priorityid"];
				}
			}
			updateSLAPlan($_POST["slaplanid"], $_POST["title"], $_POST["overduehrs"], $_POST["departmentid"], $_POST["slascheduleid"], $_POST["ticketstatusid"], $_POST["priorityid"]);

			printRedirect(sprintf($_SWIFT["language"]["planupdateconfirm"], $_POST["title"]), "index.php?_m=tickets&_a=manageplans&updateplan=".urlencode($_POST["title"]));
			exit;
		}
	}

	// ======= Fetch All The Data =======
	$_slaplan = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."slaplans` WHERE `slaplanid` = '". intval($_REQUEST["slaplanid"]) ."';");
	if (empty($_slaplan["slaplanid"]))
	{
		trigger_error($_SWIFT["language"]["invalidslaplan"], E_USER_ERROR);
	}

	$_POST = array_merge($_POST, $_slaplan);

	$_POST["ticketstatusid"] = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."slaplanstatuslink` WHERE `slaplanid` = '". intval($_REQUEST["slaplanid"]) ."';");
	while ($dbCore->nextRecord())
	{
		$_POST["ticketstatusid"][] = $dbCore->Record["ticketstatusid"];
	}

	$_POST["priorityid"] = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."slaplanprioritylink` WHERE `slaplanid` = '". intval($_REQUEST["slaplanid"]) ."';");
	while ($dbCore->nextRecord())
	{
		$_POST["priorityid"][] = $dbCore->Record["priorityid"];
	}

	// ======= Start Interface Output =======
	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["sla"]." > ".$_SWIFT["language"]["editslaplan"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageplans" title="'.$_SWIFT["language"]["slaext"].'">'.$_SWIFT["language"]["slaext"].'</a> &raquo; <a href="index.php?_m=tickets&_a=editplan&slaplanid='. intval($_slaplan["slaplanid"]) .'" title="'.$_SWIFT["language"]["edit"].'">'.$_SWIFT["language"]["edit"].'</a>', $_SWIFT["language"]["desc_sla"] , 6);
	
	// Check for schedules
	if (!count($_SWIFT["slaschedulecache"]))
	{
		$errormessage = iif($errormessage=="", $_SWIFT["language"]["errorinsertschedule"], $errormessage);
	}

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderSLAForm(EDIT);

	$template->assign("backurl", "index.php?_m=tickets&_a=manageplans");

	$interface->adminFooter();




/**
* ###############################################
* MANAGE SLA SCHEDULES
* ###############################################
*/
} else if ($eventaction == "manageschedules") {
	if ($_GET["do"] == "delete" && !empty($_GET["slascheduleid"]) && $_SWIFT["isdemo"] != true)
	{
		$result = deleteSLASchedules(array($_GET["slascheduleid"]));
		if ($result)
		{
			$infomessage = sprintf($_SWIFT["language"]["scheduledeleteconfirm"], $_SWIFT["slaschedulecache"][$_GET["slascheduleid"]]["title"]);			
		} else {
			$_SWIFT["errormessage"] = $_SWIFT["language"]["scheduledeletefailure"];
		}

	}

	if (!empty($_REQUEST["insertschedule"]))
	{
		$infomessage = sprintf($_SWIFT["language"]["scheduleinsertconfirm"], $_REQUEST["insertschedule"]);
	} else if (!empty($_REQUEST["updateschedule"])) {
		$infomessage = sprintf($_SWIFT["language"]["scheduleupdateconfirm"], $_REQUEST["updateschedule"]);
	}
	

	$options["recordsperpage"] = "3";
	$options["sortby"] = "title";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteSLASchedules";

	$options["idname"] = "slascheduleid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["scheduletitle"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'slaschedules` WHERE title LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_scheduleFields";

	$fields[0]["name"] = "title";
	$fields[0]["title"] = $_SWIFT["language"]["scheduletitle"];
	$fields[0]["width"] = "250";
	$fields[0]["valign"] = "top";
	$fields[0]["align"] = "left";

	$fields[1]["type"] = "custom";
	$fields[1]["name"] = "scheduledesc";
	$fields[1]["title"] = $_SWIFT["language"]["scheduledesc"];
	$fields[1]["align"] = "left";
	$fields[1]["valign"] = "top";

	$fields[2]["type"] = "custom";
	$fields[2]["name"] = "options";
	$fields[2]["title"] = $_SWIFT["language"]["options"];
	$fields[2]["width"] = "150";
	$fields[2]["align"] = "center";
	$fields[2]["valign"] = "top";

	$grid = new Grid();

	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["sla"]." > ".$_SWIFT["language"]["manageschedules"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageplans" title="'.$_SWIFT["language"]["slaext"].'">'.$_SWIFT["language"]["slaext"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageschedules" title="'.$_SWIFT["language"]["manageschedules"].'">'.$_SWIFT["language"]["manageschedules"].'</a>', $_SWIFT["language"]["desc_sla"] , 6);

	$grid->start("slaschedules", $_SWIFT["language"]["slaschedulelist"], 'SELECT * FROM `'. TABLE_PREFIX .'slaschedules` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'slaschedules`;', 'SELECT * FROM `'. TABLE_PREFIX .'slaschedules` WHERE title LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);

	printInfoBox($infomessage);
	printErrorBox($_SWIFT["errormessage"]);
	
	$grid->display("slaschedules");
	
	$interface->adminFooter();






/**
* ###############################################
* INSERT SCHEDULE
* ###############################################
*/
} else if ($eventaction == "insertschedule") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			$dateerror = false;
			$_args = array();
			foreach ($_sladays as $key=>$val)
			{
				$dayopen = $_POST[$val."_open"];
				$dayclose = $_POST[$val."_close"];
				$hourdiff = checkHourRange($dayopen, $dayclose);
				if ($hourdiff == -1)
				{
					$dateerror = true;
				} else {
					$_args[$val."_enabled"] = iif($_POST[$val."_enabled"] == 1 && $hourdiff != 0, "1", "0");
					$_args[$val."_open"] = $dayopen;
					$_args[$val."_close"] = iif($_args[$val."_enabled"]=="0", "00:00", $dayclose);
				}
			}

			if ($dateerror)
			{
				$errormessage = $_SWIFT["language"]["hourrangeerror"];
			} else {
				// All ok.. hopefully..
				$slascheduleid = insertSLASchedule($_POST["title"], $_args);
				printRedirect(sprintf($_SWIFT["language"]["scheduleinsertconfirm"], $_POST["title"]), "index.php?_m=tickets&_a=manageschedules&insertschedule=".urlencode($_POST["title"]));
				exit;
			}

		}
	}

	// ======= Start Interface Output =======
	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["sla"]." > ".$_SWIFT["language"]["insertslaschedule"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageplans" title="'.$_SWIFT["language"]["slaext"].'">'.$_SWIFT["language"]["slaext"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageschedules" title="'.$_SWIFT["language"]["manageschedules"].'">'.$_SWIFT["language"]["manageschedules"].'</a> &raquo; <a href="index.php?_m=tickets&_a=insertschedule" title="'.$_SWIFT["language"]["insert"].'">'.$_SWIFT["language"]["insert"].'</a>', $_SWIFT["language"]["desc_sla"] , 6);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderSLAScheduleForm(INSERT);

	$template->assign("backurl", "index.php?_m=tickets&_a=manageschedules");

	$interface->adminFooter();




/**
* ###############################################
* EDIT SCHEDULE
* ###############################################
*/
} else if ($eventaction == "editschedule") {
	if ($_POST["step"] == "1")
	{
		if (trim($_POST["title"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else if ($_SWIFT["isdemo"] == true) {
			$errormessage = $_SWIFT["language"]["demomode"];
		} else {
			$dateerror = false;
			$_args = array();
			foreach ($_sladays as $key=>$val)
			{
				$dayopen = $_POST[$val."_open"];
				$dayclose = $_POST[$val."_close"];
				$isenabled = iif($_POST[$val."_enabled"] == 1, true, false);

				$hourdiff = checkHourRange($dayopen, $dayclose);
				if ($hourdiff == -1 && $isenabled)
				{
					$dateerror = true;
				} else {
					$_args[$val."_enabled"] = iif($_POST[$val."_enabled"] == 1 && $hourdiff != 0, "1", "0");
					$_args[$val."_open"] = $dayopen;
					$_args[$val."_close"] = iif($_args[$val."_enabled"]=="0", "00:00", $dayclose);
				}
			}

			if ($dateerror)
			{
				$errormessage = $_SWIFT["language"]["hourrangeerror"];
			} else {
				// All ok.. hopefully..
				updateSLASchedule($_POST["slascheduleid"], $_POST["title"], $_args);

				printRedirect(sprintf($_SWIFT["language"]["scheduleupdateconfirm"], $_POST["title"]), "index.php?_m=tickets&_a=manageschedules&updateschedule=".urlencode($_POST["title"]));

				exit;
			}

		}
	}

	// ======= Fetch Schedule Data =======
	$_slaschedule = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."slaschedules` WHERE `slascheduleid` = '". intval($_REQUEST["slascheduleid"]) ."';");
	if (empty($_slaschedule["slascheduleid"]))
	{
		trigger_error($_SWIFT["language"]["invalidslaschedule"], E_USER_ERROR);
	}

	$_POST = array_merge($_POST, $_slaschedule);

	// ======= Start Interface Output =======
	$interface->adminHeader($_SWIFT["language"]["tickets"]." > ".$_SWIFT["language"]["sla"]." > ".$_SWIFT["language"]["editslaschedule"], 1);

	$interface->adminNavBar('<a href="index.php?_m=tickets&_a=managestatus" title="'.$_SWIFT["language"]["tickets"].'">'.$_SWIFT["language"]["tickets"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageplans" title="'.$_SWIFT["language"]["slaext"].'">'.$_SWIFT["language"]["slaext"].'</a> &raquo; <a href="index.php?_m=tickets&_a=manageschedules" title="'.$_SWIFT["language"]["manageschedules"].'">'.$_SWIFT["language"]["manageschedules"].'</a> &raquo; <a href="index.php?_m=tickets&_a=editschedule&slascheduleid='. intval($_slaschedule["slascheduleid"]) .'" title="'.$_SWIFT["language"]["edit"].'">'.$_SWIFT["language"]["edit"].'</a>', $_SWIFT["language"]["desc_sla"] , 6);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderSLAScheduleForm(EDIT);

	$template->assign("backurl", "index.php?_m=tickets&_a=manageschedules");

	$interface->adminFooter();
}
?>